package com.shangbang.verb.service.impl.recruit;

import com.shangbang.verb.dao.recruit.NounMapper;
import com.shangbang.verb.dao.recruit.UserMapper;
import com.shangbang.verb.entity.recruit.NounEntity;
import com.shangbang.verb.entity.recruit.UserEntity;
import com.shangbang.verb.service.recruit.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NounMapper nounMapper;
    @Override
    public Object createUserByType(String phone, String nickname, Integer gender, String aboutMe, String wxuid, String icon, Integer type) {
        //type:0 手机号注册，type：1 微信注册
        Object result = "";
        if (type == 0) {
            if (StringUtils.isNoneBlank(phone)) {
                result = createUser(phone, nickname, gender, aboutMe, wxuid, icon);
            }
        } else if (type == 1) {
            if (StringUtils.isNoneBlank(wxuid)) {
                result = createUser(phone, nickname, gender, aboutMe, wxuid, icon);
            }
        }

        return result;

    }

    @Transactional(readOnly = false)
    public Object createUser(String phone, String nickname, Integer gender, String aboutMe, String wxuid, String icon) {
        UserEntity userEntity = new UserEntity();
        userEntity.setPhone(phone);
        userEntity.setNickname(nickname);
        userEntity.setGender(gender);
        userEntity.setAboutMe(aboutMe);
        userEntity.setWxuid(wxuid);
        userEntity.setIcon(icon);
        userMapper.createUser(userEntity);
        Integer id = userEntity.getId();
        if (null != id) {
            UserEntity user = userMapper.selectById(id);
            NounEntity nounEntity = nounMapper.selectByUid(id);
            if (nounEntity==null){
                nounEntity = new NounEntity();
                nounEntity.setUserDefault("");
                nounEntity.setWorkDefault("");
                nounEntity.setUid(id);
                nounMapper.insert(nounEntity);
            }
            return user;
        }
        return "";

    }

    @Override
    public Object getUserState(String phone, String wxuid) {
        if (StringUtils.isBlank(phone)) {
            phone = "";
        }
        if (StringUtils.isBlank(wxuid)) {
            wxuid = "";
        }
        List<UserEntity> userList = userMapper.getUserListbyPhoneOrWxuid(phone, wxuid);
        if (userList != null && userList.size() > 0) {

            return userList.get(0);
        }

        return "create";
    }

    @Override
    public Object UpdateUserByType(Integer uid, String phone, String nickname, Integer gender, String aboutMe, String wxuid, String icon) {
        UserEntity userEntity = userMapper.selectById(uid);
        userEntity.setPhone(phone);
        userEntity.setNickname(nickname);
        userEntity.setGender(gender);
        userEntity.setAboutMe(aboutMe);
        userEntity.setWxuid(userEntity.getWxuid());
        userEntity.setIcon(userEntity.getIcon());


        List<UserEntity> userList = userMapper.getUserListbyPhoneOrWxuid(userEntity.getPhone(), "");
        if (userList != null && userList.size() > 1) {

            return "";
        }
        Integer id = userMapper.updateById(userEntity);
        return userEntity;
    }

    @Override
    public Object getUserInfo(Integer uid) {
        UserEntity userEntity = userMapper.selectById(uid);
        if (null != userEntity) {

            return userEntity;
        }
        return "";

    }
}